﻿using BCCommon;
using BCEntity.TMS.TrainingSite.TrainingSiteConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.TMS.TrainingSite.TrainingSiteConfig
{
    public class TrainingSiteConfigData : ITrainingSiteConfigData
    {
        private readonly IDatabaseContext database;

        public TrainingSiteConfigData(IDatabaseContext database)
        {
            this.database = database;
        }

        private const string QUERY = @"SELECT
										TrainingSiteConfigId,
										AgentRecordId,
										TrainingSiteName,
										TrainingSiteAddress,
										ProvinceCode,
										ProvinceName,
										CityCode,
										CityName,
										CountyCode,
										CountyName,
										Phone,
										AppointmentAvailable,
										CreateTime,
										CreateAdminId,
										CreateAdminName,
										UpdateTime,
										EditAdminId,
										EditAdminName 
									FROM
										TrainingSiteConfig";

        public async Task<TrainingSiteConfigEntity> Insert(TrainingSiteConfigEntity entity)
        {
            return await database.Master.InsertAsync(entity, database.Transaction);
        }

        public async Task<TrainingSiteConfigEntity> GetEntity(long trainingSiteConfigId)
        {
            return await database.Slave.QueryEntityAsync<TrainingSiteConfigEntity>($"{QUERY} WHERE TrainingSiteConfigId = ?TrainingSiteConfigId", new { trainingSiteConfigId });
        }

        public async Task<TrainingSiteConfigEntity> Update(TrainingSiteConfigEntity entity)
        {
            return await database.Master.UpdateAsync(entity, database.Transaction);
        }

        public async Task<int> Delete(long trainingSiteConfigId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM TrainingSiteConfig WHERE TrainingSiteConfigId = ?TrainingSiteConfigId", new { trainingSiteConfigId }, database.Transaction);
        }

        public async Task<(IEnumerable<TrainingSiteConfigEntity>, int)> Query(long? agentRecordId, string trainingSiteName, string provinceCode, string cityCode, string countyCode, string phone, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (agentRecordId.HasValue)
            {
                wheres.Add("AgentRecordId = ?AgentRecordId");
                parameters.Add("?AgentRecordId", agentRecordId.Value, DbType.Int64);
            }
            if (!string.IsNullOrWhiteSpace(trainingSiteName))
            {
                wheres.Add("TrainingSiteName LIKE ?TrainingSiteName");
                parameters.Add("?TrainingSiteName", $"%{trainingSiteName}%", DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(provinceCode))
            {
                wheres.Add("ProvinceCode = ?ProvinceCode");
                parameters.Add("?ProvinceCode", provinceCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(cityCode))
            {
                wheres.Add("CityCode = ?CityCode");
                parameters.Add("?CityCode", cityCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(countyCode))
            {
                wheres.Add("CountyCode = ?CountyCode");
                parameters.Add("?CountyCode", countyCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(phone))
            {
                wheres.Add("Phone LIKE ?Phone");
                parameters.Add("?Phone", $"%{phone}%", DbType.AnsiString);
            }
            string sql = string.Format(@"{0} {1} ORDER BY TrainingSiteConfigId DESC LIMIT {2},{3};
                        SELECT COUNT(1) FROM TrainingSiteConfig {1}",
                        QUERY,
                        wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                        (pageIndex - 1) * pageSize,
                        pageSize);
            return await database.Slave.QueryPageableListAsync<TrainingSiteConfigEntity>(sql, parameters);
        }

        public async Task<IEnumerable<TrainingSiteConfigEntity>> GetList(long? agentRecordId, string countyCode)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (agentRecordId.HasValue)
            {
                wheres.Add("AgentRecordId = ?AgentRecordId");
                parameters.Add("?AgentRecordId", agentRecordId.Value, DbType.Int64);
            }
            if (!string.IsNullOrWhiteSpace(countyCode))
            {
                wheres.Add("CountyCode = ?CountyCode");
                parameters.Add("?CountyCode", countyCode, DbType.AnsiString);
            }
            string sql = string.Format("{0} {1}", QUERY, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty);
            return await database.Slave.QueryAsync<TrainingSiteConfigEntity>(sql, parameters);
        }
    }
}
